"use strict";

var Fly = require("../../dist/npm/fly");
var EngineWrapper = require("../../dist/npm/engine-wrapper");
var adapter = require("../adapter/node");
var utils = require("../utils/utils");
var nodeEngine = EngineWrapper(adapter);
var fs = require("fs");
var path = require("path");
var request = require("request");
var rq = request.defaults({ jar: true });
Object.assign(Fly.prototype, {

    // Http plugin
    "$http": rq,

    // File download API
    download: function download(url, savePath) {
        var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
        var options = arguments[3];

        return this.request(url, params, utils.merge({ responseType: "stream" }, options)).then(function (d) {
            return new Promise(function (resolve, reject) {
                fs.writeFile(savePath, d.data, function (err) {
                    if (!err) {
                        resolve({ size: d.data.length, path: path.resolve(savePath) });
                    } else {
                        // Failed to save file
                        err.status = 2;
                        reject(err);
                    }
                });
            });
        })["catch"](function (e) {
            return Promise.reject(e);
        });
    },


    // File upload API
    upload: function upload(url, formData, options) {
        return this.post(url, null, utils.merge({ formData: formData }, options));
    }
});

module.exports = function (engine) {
    return new Fly(engine || nodeEngine);
};